﻿using System.ComponentModel;
using ZeroSum.App.Datas;
using ZeroSumZSA.Api.Apis;
using ZeroSumZSA.Application.ApplicationServices.Users;
using ZeroSumZSA.Application.ApplicationServices.Users.Dto.In;
using ZeroSumZSA.Application.ApplicationServices.Users.Dto.Out;
using ZeroSumZSA.Application.ApplicationServices.Users.Dto.Query;

namespace ZeroSumZSA.Api.Controllers;

/// <summary>
///     管理-用户管理
/// </summary>
[Description("管理-用户管理")]
public class UserController : ApiController
{
    private readonly IUserApplicationService _service;

    /// <summary>
    ///     管理-用户管理
    /// </summary>
    /// <param name="service"></param>
    public UserController(IUserApplicationService service)
    {
        _service = service;
    }

    /// <summary>
    ///     分页查询用户
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<ApiResponse<PagedResponseModel<PageUserDto>>> Page([FromQuery] PageUserQuery query)
    {
        var users = await _service.Page(query);
        return ApiResponse<PagedResponseModel<PageUserDto>>.Of(users);
    }

    /// <summary>
    ///     增加用户
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<ApiResponse> Add([FromBody] AddUserDto dto)
    {
        await _service.AddAsync(dto);
        return ApiResponse.Ok();
    }

    /// <summary>
    ///     删除用户
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<ApiResponse> Delete([FromBody] DeleteUserDto dto)
    {
        await _service.DeleteAsync(dto);
        return ApiResponse.Ok();
    }

    /// <summary>
    ///     更新用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<ApiResponse> Edit([FromBody] EditUserInput input)
    {
        await _service.UpdateAsync(input);
        return ApiResponse.Ok();
    }

    /// <summary>
    ///     查看用户
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<ApiResponse<DetailUserDto>> Detail([FromQuery] DetailUserQuery query)
    {
        var userDetail = await _service.Detail(query);
        return ApiResponse<DetailUserDto>.Of(userDetail);
    }
}